#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"

cmd-report() {
  declare desc="reports dokku vitals for troubleshooting"
  declare cmd="report"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1"

  dokku_log_info1 "uname: $(uname -a)"
  dokku_log_info1 "memory: "
  free -m | sed "s/^/       /"
  dokku_log_info1 "docker version: "
  "$DOCKER_BIN" version | sed "s/^/       /"
  dokku_log_info1 "docker daemon info: "
  "$DOCKER_BIN" -D system info | sed "s/^/       /"
  dokku_log_info1 "sigil version: $(sigil -v)"
  dokku_log_info1 "herokuish version: "
  "$DOCKER_BIN" container run $DOKKU_GLOBAL_RUN_ARGS --rm "$DOKKU_IMAGE" herokuish version | sed "s/^/       /"
  dokku_log_info1 "dokku version: $(dokku version)"
  dokku_log_info1 "dokku plugins: "
  dokku plugin:list | sed "s/^/       /"

  if [[ "$APP" == "--all" ]]; then
    for app in $(dokku_apps); do
      plugn trigger report "$app"
    done
  elif [[ -n "$APP" ]]; then
    plugn trigger report "$APP"
  fi
}

cmd-report "$@"
